package com.buaa.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.buaa.common.Result;
import com.buaa.dto.LocationDTO;
import com.buaa.dto.TrafficIncidentDTO;
import com.buaa.entity.User;
import com.buaa.entity.UserExtension;
import com.buaa.enums.ResponseCode;
import com.buaa.service.impl.TrafficIncidentServiceImpl;
import com.buaa.service.impl.UserExtensionServiceImpl;
import com.buaa.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userpersona")
/*
 * 用户登录控制类
 * @apiNote 交警画像
 * @author 1146,
 * @Date 2024/6/29
 */
public class TrafficPolicePersonaCtl {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UserExtensionServiceImpl extService;

    @GetMapping("/job")
    public Result showUserByJob() {
        Map map = new HashMap();
        List<LocationDTO>  location = extService.listUserByJob();
        map.put("JOB", location);
        return Result.success(ResponseCode.ECHARTS_SUCCESS.getKey(), ResponseCode.ECHARTS_SUCCESS.getDesc(), location);
    }

    @GetMapping("/progress")
    public Result showProgress(){
        List<TrafficIncidentDTO> dto =  trafficIncidentService.getIncidentByMonth();
        Map map = new HashMap();
        map.put("char2",dto);
        map.put("open", trafficIncidentService.countOpenCase());
        map.put("close", trafficIncidentService.countClosedCase());
        map.put("users", userService.count());
        return Result.success(ResponseCode.ECHARTS_SUCCESS.getKey(), ResponseCode.ECHARTS_SUCCESS.getDesc(), map);
    }

    @Autowired
    private TrafficIncidentServiceImpl trafficIncidentService;
    @GetMapping("/age")
    public Result showUserAttributesByAge() {
        Map map = new HashMap();
        List<LocationDTO>  location = extService.listUserAttributesByAge();
        map.put("LOCATION", location);
        return Result.success(ResponseCode.ECHARTS_SUCCESS.getKey(), ResponseCode.ECHARTS_SUCCESS.getDesc(), location);
    }

    @GetMapping("/location")
    public Result showUserAttributesByLocation() {
        Map map = new HashMap();
        List<LocationDTO>  location = extService.listUserAttributesByLocation();
        map.put("LOCATION", location);
        return Result.success(ResponseCode.ECHARTS_SUCCESS.getKey(), ResponseCode.ECHARTS_SUCCESS.getDesc(), location);
    }
    @GetMapping("/workrangeByPie")
    public Result workRangeByPie(){
        List<User> list=userService.list();
        int q1=0;//分别定义四个季度
        int q2=0;
        int q3=0;
        int q4=0;
// 转换为Date
        ZoneId zoneId = ZoneId.systemDefault(); // 或者使用特定的时区，如ZoneId.of("Europe/London")
        for(User user:list){
            LocalDateTime createTime=user.getCreateTime();
            ZonedDateTime zonedDateTime = createTime.atZone(zoneId);
            Date date = Date.from(zonedDateTime.toInstant());
            Quarter quarter= DateUtil.quarterEnum(date);
            switch (quarter){
                case Q1:q1+=1;break;
                case Q2:q2+=1;break;
                case Q3:q3+=1;break;
                case Q4:q4+=1;break;
                default:break;
            }
        }
        return Result.success(CollUtil.newArrayList(q1,q2,q3,q4));
    }

    // 对应 交警画像-时域， 加入交警年限 界面的数据 ,  table: police_user_extension
    @GetMapping("/yearsofservice")
    public Result showUserAttributes() {
        List<UserExtension> list = extService.list();
        int m1 = 0;//分别定义 range
        int m2 = 0;
        int m3 = 0;
        int m4 = 0;
        int m5 = 0;
        int m6 = 0;
        int m7 = 0;
        int m8 = 0;
        int m9 = 0;
// 转换为Date
        ZoneId zoneId = ZoneId.systemDefault(); // 或者使用特定的时区，如ZoneId.of("Europe/London")
        for (UserExtension user : list) {
            LocalDateTime createTime = user.getJoinDate();
            ZonedDateTime zonedDateTime = createTime.atZone(zoneId);
            Date date = Date.from(zonedDateTime.toInstant());
            Date currentDate = new Date();
            int years = (int) DateUtil.betweenYear(currentDate, date, false);

            if (years <= 1) {
                m1 += 1;
            }
            if (years <= 3) {
                m2 += 1;
            }
            if (years >= 5) {
                m3 += 1;
            }
            if (years >= 10) {
                m4 += 1;
            }
            if (years >= 15) {
                m5 += 1;
            }
            if (years >= 20) {
                m6 += 1;
            }
            if (years >= 25) {
                m7 += 1;
            }
            if (years >= 30) {
                m8 += 1;
            }
            if (years >= 35) {
                m9 += 1;
            }
        }

        //
        List<TrafficIncidentDTO> dto = trafficIncidentService.getIncidentByMonth();
        Map map = new HashMap();
        map.put("char1", CollUtil.newArrayList(m1, m2, m3, m4, m5, m6, m7, m8, m9));
        map.put("char2", dto);
        map.put("open", trafficIncidentService.countOpenCase());
        map.put("close", trafficIncidentService.countClosedCase());
        map.put("users", userService.count());
        return Result.success(ResponseCode.ECHARTS_SUCCESS.getKey(), ResponseCode.ECHARTS_SUCCESS.getDesc(), map);
    }
}

